#include<bits/stdc++.h>
using namespace std;
const int N=200005;
int n,q,c,fi[N],co[N];
struct tree
{
    vector<int>f,sz;
    int gf(int x){return x==f[x]?x:f[x]=gf(f[x]);}
    void init(int n,int S)
    {
        sz=f=vector<int>(n+1);
        for(int i=1;i<=n;++i)sz[i]=1,f[i]=i;
        for(int i=2;i<=n;++i)
        {
            if(((1<<co[i])&S)&&((1<<co[fi[i]])&S))
            {
                int fx=gf(i),fy=gf(fi[i]);
                sz[fy]+=sz[fx];
                f[fx]=fy;
            }
        }
    }
    int qus(int u){return sz[gf(u)];}
};
vector<tree>T;
vector<int>p[N];
int dfs(int x,int S,int fa)
{
    int sum=0;
    for(int to:p[x])
    {
        if(to==fa)continue;
        if((1<<co[to])&S)sum+=dfs(to,S,x);
    }
    return sum+1;
}
int main()
{
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>q>>c;
    for(int i=1;i<=n;++i)cin>>co[i];
    for(int i=2;i<=n;++i)cin>>fi[i];
    if(n*q<=100000000)
    {
        for(int i=2;i<=n;++i)p[i].push_back(fi[i]),p[fi[i]].push_back(i);
        for(int i=1,s,u;i<=q;++i)
        {
            cin>>s>>u;
            if((1<<co[u])&s)cout<<"0\n";
            else cout<<dfs(u,s,0)<<"\n";
        }
    }
    else
    {
        T=vector<tree>(1<<c);
        for(int s=0;s<(1<<c);++s)T[s].init(n,s);
        for(int i=1,s,u;i<=q;++i)
        {
            cin>>s>>u;
            if((1<<co[u])&s)cout<<"0\n";
            else cout<<T[s].qus(u)<<"\n";
        }
    }
    return 0;
}